import { Locale as VarletLocale } from '@varlet/ui';
import { createI18n } from 'vue-i18n';

export function loadLang() {
  const modules: Record<string, any> = import.meta.glob('./lang/*.ts', { eager: true });
  const langs: Record<string, any> = {};

  for (const path in modules) {
    const name = path.replace(/(\.\/lang\/|\.ts)/g, '');
    langs[name] = modules[path].lang;
  }
  return langs;
}
//默认使用韩文

const localLang = ['ENGLISH', 'INDONESIAN', 'TAMIL', 'TELUGU', 'CHINESE', 'TRADITIONALCHINESE'];
const isdui = localLang.some((item) => {
  return item == (localStorage.getItem('lang') as string);
});
if (!localStorage.getItem('lang') || !isdui) {
  localStorage.setItem('lang', 'ENGLISH');
}

export const CURRENT_LANG = localStorage.getItem('lang') as string;

// 设置 varlet 的默认语言
VarletLocale.add('en-US', VarletLocale.enUS);
VarletLocale.use('en-US');
export const i18n = createI18n({
  // globalInjection: true,
  legacy: true,
  locale: CURRENT_LANG,
  fallbackLocale: CURRENT_LANG,
  messages: loadLang(),
});

/**
 * 可以使用该方法在组件外部翻译
 * @param val
 * @returns
 */
export const i18nTranslate = (val: string) => {
  // @ts-ignore
  return i18n.global.t(val);
};

export function setLang(locale?: string) {
  if (locale) {
    localStorage.setItem('lang', locale);
    // Locale.use(locale.toLowerCase());
    // VarletLocale.use(locale);
  }
  i18n.global.locale = locale || localStorage.getItem('lang') || '';
}

export const LANG_CONFIG = [
  { name: '简体中文', lang: 'CHINESE' },
  { name: '繁體中文', lang: 'TRADITIONALCHINESE' },
  { name: 'English', lang: 'ENGLISH' },
  // { name: 'हिन्दी', lang: 'INDONESIAN' },
  // { name: 'தமிழ்', lang: 'TAMIL' },
  // { name: 'తెలుగు', lang: 'TELUGU' },
];
    